package org.zffc.common.util;


import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 客户端工具类
 *
 */
public class ServletUtils
{
    private ServletUtils() {
    }

    /**
     * 获取String参数
     */
    public static String getParameter(String name)
    {
        HttpServletRequest request = getRequest();
        if(null != request){
            return request.getParameter(name);
        }
        return null;
    }

    /**
     * 获取String参数
     */
    public static String getParameter(String name, String defaultValue)
    {
        HttpServletRequest request = getRequest();
        if(null != request){
            return ConvertUtils.toStr(request.getParameter(name), defaultValue);
        }
        return null;
    }

    /**
     * 获取Integer参数
     */
    public static Integer getParameterToInt(String name)
    {
        HttpServletRequest request = getRequest();
        if(null != request){
            return ConvertUtils.toInt(request.getParameter(name));
        }
        return null;
    }

    /**
     * 获取Integer参数
     */
    public static Integer getParameterToInt(String name, Integer defaultValue)
    {
        HttpServletRequest request = getRequest();
        if(null != request){
            return ConvertUtils.toInt(request.getParameter(name), defaultValue);
        }
        return null;
    }

    /**
     * 获取request
     */
    public static HttpServletRequest getRequest()
    {
        ServletRequestAttributes request = getRequestAttributes();
        if(null != request) {
            return request.getRequest();
        }
        return  null;
    }

    /**
     * 获取response
     */
    public static HttpServletResponse getResponse()
    {
        ServletRequestAttributes attributes = getRequestAttributes();
        if(null != attributes) {
            return attributes.getResponse();
        }
        return null;
    }

    /**
     * 获取session
     */
    public static HttpSession getSession()
    {
        HttpServletRequest request = getRequest();
        if(null != request){
            return request.getSession();
        }
        return null;
    }

    public static ServletRequestAttributes getRequestAttributes()
    {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }


    /**
     * 是否是Ajax异步请求
     * 
     * @param request
     */
    public static boolean isAjaxRequest(HttpServletRequest request)
    {
        String accept = request.getHeader("accept");
        if (accept != null && accept.indexOf("application/json") != -1)
        {
            return true;
        }

        String xRequestedWith = request.getHeader("X-Requested-With");
        if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
        {
            return true;
        }

        String uri = request.getRequestURI();
        if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
        {
            return true;
        }

        String ajax = request.getParameter("__ajax");
        if (StringUtils.inStringIgnoreCase(ajax, "json", "xml"))
        {
            return true;
        }
        return false;
    }
}
